﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using FineUICore;
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace MyAppBox.Area.system.Controllers
{
    [Area("system")]
    public class system6Controller : SysPageController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            InitPageData();
            return View();
        }

        /// <summary>
        /// 按钮编辑页面
        /// </summary>
        /// <returns></returns>
        public ActionResult sys8Edit(string id, string str40)
        {
            Initsys8EditData(id, str40);
            return View("sys8Edit");
        }

        #region Index页面
        /// <summary>
        /// 绘制树及其他数据
        /// </summary>
        private void InitPageData()
        {
            DataTable dt = GetData(null);
            //绑定树
            IList<TreeNode> nodes = new List<TreeNode>();
            nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_6_20", "SYSTEM_6_10", "SYSTEM_6_70");
            ViewBag.TreeMenuNodes = nodes.ToArray();
            //绑定下拉控件
            ViewBag.ddlsys6_70DataSource = FineUIEx.GetNewTreeDt(dt, "SYSTEM_6_10", "SYSTEM_6_70");
            var EditForm = UIHelper.Form("EditForm");
            EditForm.FieldReadOnlyAll(true);
        }

        /// <summary>
        /// 得到数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetData(MsQuery ms)
        {
            if (ms == null) { ms = new SysFrame.Message.MsQuery(); }
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 and SYSTEM_6_30='1' ";
            }
            //ms.strorder = "  ";//排序语句
            DataTable dt = ((DataSet)new BLLsystem_6().Getsystem_6ByQuery(ms).data).Tables[0];
            return dt;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="ms">页面实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys6save_Click(JObject ms)
        {
            Mssystem_6 ms6 = (Mssystem_6)ms.ToObject(typeof(Mssystem_6));
            ms6.System_6_30 = "1";
            ms6.System_6_50 = "1";
            ms6.System_6_100 = "0";
            MsReturned rt = new BLLsystem_6().Savesystem_6(ms6);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                var treeMenu = UIHelper.Tree("treeMenu");
                DataTable dt = GetData(null);
                //绑定树
                IList<TreeNode> nodes = new List<TreeNode>();
                nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_6_20", "SYSTEM_6_10", "SYSTEM_6_70");
                treeMenu.LoadData(nodes);
                //重置选择
                string strscript = " F.ui.EditForm.DataID='" + rt.strMsId + "';";
                treeMenu.SelectedNodeID(rt.strMsId);
                PageContext.RegisterStartupScript(strscript);
                ShowNotify("保存成功");
            }
            return UIHelper.Result();

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="System_6_10">ID</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys6Del_Click(string System_6_10)
        {
            MsReturned rt = new BLLsystem_6().Deletesystem_6ByKey(System_6_10);
            //if (rt.MsRbool)
            {
                var treeMenu = UIHelper.Tree("treeMenu");
                DataTable dt = GetData(null);
                //绑定树
                IList<TreeNode> nodes = new List<TreeNode>();
                nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_6_20", "SYSTEM_6_10", "SYSTEM_6_70");
                treeMenu.LoadData(nodes);
                //treeMenu.SelectedNodeID(dt.Rows[0]["SYSTEM_6_10"].ToString());
                string strscript = "$(F.ui.treeMenu.el).find('.f-tree-node').eq(1).click();";
                PageContext.RegisterStartupScript(strscript);
                ShowNotify(rt.strMS);
            }
            return UIHelper.Result();
        }

        /// <summary>
        /// 树节点点击事件
        /// </summary>
        /// <param name="nodeId">点击ID</param>
        /// <param name="fields">Grid字段</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Tree1_NodeClick(string nodeId, JArray fields)
        {
            Mssystem_6 listms = new BLLsystem_6().Getsystem_6MsByKey(nodeId);
            //if (listms.System_6_50 != "1")//不能修改不能删除
            //{
            //    btnsys6del.Enabled = false;
            //    btnsys6edit.Enabled = false;
            //}
            //else
            //{
            //    btnsys6del.Enabled = true;
            //    btnsys6edit.Enabled = true;
            //}
            //if (lodeuser.isadmin)
            //{
            //    btnsys6del.Enabled = true;
            //    btnsys6edit.Enabled = true;
            //}
            //btnsys6save.Enabled = false;
            //btnsys6savenew.Enabled = false;
            //btnsys6close.Enabled = false;
            //Grid1.Enabled = true;
            UIHelper.TextBox("txtsys6_20").Text(listms.System_6_20);
            UIHelper.TextBox("txtsys6_80").Text(listms.System_6_80);
            UIHelper.TextBox("txtsys6_40").Text(listms.System_6_40);
            UIHelper.DropDownList("ddlsys6_70").SelectedValue(listms.System_6_70);
            GetGridData(nodeId, fields);

            var EditForm = UIHelper.Form("EditForm");
            EditForm.FieldReadOnlyAll(true);
            return UIHelper.Result();
        }

        /// <summary>
        /// 得到数据源
        /// </summary>
        /// <param name="nodeId">父ID</param>
        /// <param name="fields">字段名称</param>
        private void GetGridData(string nodeId, JArray fields)
        {
            var Grid1 = UIHelper.Grid("Grid1");
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 and (SYSTEM_8_50='1' or SYSTEM_8_50 is null) ";
            }
            ms.strquery += " and SYSTEM_8_40='" + nodeId + "'";
            ms.strorder = " SYSTEM_8_60 ";//排序语句
            //List<Mssystem_8> listms = new BLLsystem_8().Getsystem_8MsByQuery(ms);
            DataTable dt = ((DataSet)new BLLsystem_8().Getsystem_8ByQuery(ms).data).Tables[0];
            //绑定数据
            Grid1.DataSource(dt, fields);
        }

        #endregion

        #region sys8Edit
        /// <summary>
        /// 绘制页面
        /// </summary>
        private void Initsys8EditData(string id, string str40)
        {
            ViewBag.str10 = id != null ? id : "0";
            ViewBag.str40 = str40;
            Mssystem_8 ms8 = new Mssystem_8();
            if (id != null)
            {
                ms8 = new BLLsystem_8().Getsystem_8MsByKey(id);
            }
            ViewBag.data = JObject.FromObject(ms8).ToString();
            string strscript = " F.ui.EditForm.DataID='" + id + "';";
            PageContext.RegisterStartupScript(strscript);
        }

        /// <summary>
        /// 按钮保存
        /// </summary>
        /// <param name="ms">页面实体</param>
        /// <param name="callback">保存完后回调</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult save_Click(JObject ms, string callback)
        {
            Mssystem_8 ms8 = (Mssystem_8)ms.ToObject(typeof(Mssystem_8));
            ms8.System_8_50 = "1";//是否有效
            MsReturned rt = new BLLsystem_8().Savesystem_8(ms8);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                //重置选择
                string strscript = " F.ui.EditForm.DataID='" + rt.strMsId + "';";
                strscript += callback;
                PageContext.RegisterStartupScript(strscript);
            }
            return UIHelper.Result();
        }

        /// <summary>
        /// 按钮删除
        /// </summary>
        /// <param name="ids">要删除的ID</param>
        /// <param name="callback">保存完后回调</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult btndelete_Click(string ids, string callback)
        {
            MsQuery msq = new MsQuery();
            msq.strquery = " and System_8_10 in (" + ids + ")";
            MsReturned msr = new BLLsystem_8().Deletesystem_8ByQuery(msq);
            ShowNotify(msr.strMS);
            string strscript = "";
            strscript += callback;
            PageContext.RegisterStartupScript(strscript);
            return UIHelper.Result();

        }
        #endregion
    }
}